# coding=utf-8
# author=haishan09@gmail.com
from __future__ import unicode_literals
import json
from django.db import models
from dimension.models import Dimension, Widget
from cube.models import Cube
from chart.models import Chart
from custom.fields import JsonifyConfigField
from treemenu.models import MenuItem


class Report(models.Model):
    label = models.CharField('标题', max_length=128)
    menuitem = models.ForeignKey(MenuItem, related_name='related_reports', blank=True, null=True)
    cuts = models.ManyToManyField(
        Dimension, related_name='related_reports', blank=True, through='CutShip')
    created_at = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.label


class CubeShip(models.Model):
    label = models.CharField('标题', max_length=128)
    cube = models.ForeignKey(Cube, verbose_name='数据来源')
    chart = models.ForeignKey(Chart, verbose_name='可视化类型')
    config = JsonifyConfigField('配置信息')
    limit = models.IntegerField('返回记录条数限制', default=1000)
    created_at = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.label

    @property
    def dconf(self):
        return json.loads(self.config or '{}')
  

class CutShip(models.Model):
    report = models.ForeignKey(Report)
    cut = models.ForeignKey(Dimension)
    widget = models.ForeignKey(Widget)
    config = JsonifyConfigField('配置信息')
    rank = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.cut.label

    @property
    def dconf(self):
        return json.loads(self.config)

